package com.rfid.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.rfid.common.constant.CodeConstants;
import com.rfid.common.core.domain.entity.SysDictData;
import com.rfid.common.exception.BusinessException;
import com.rfid.common.utils.ShiroUtils;
import com.rfid.common.utils.StringUtils;
import com.rfid.framework.web.domain.server.Sys;
import com.rfid.framework.web.service.CodeGenerateService;
import com.rfid.framework.web.service.DictService;
import io.swagger.models.auth.In;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rfid.common.annotation.Log;
import com.rfid.common.enums.BusinessType;
import com.rfid.system.domain.TCustomerInfo;
import com.rfid.system.service.ITCustomerInfoService;
import com.rfid.common.core.controller.BaseController;
import com.rfid.common.core.domain.AjaxResult;
import com.rfid.common.utils.poi.ExcelUtil;
import com.rfid.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户信息Controller
 *
 * @author ruoyi
 * @date 2020-11-09
 */
@Controller
@RequestMapping("/system/info")
public class TCustomerInfoController extends BaseController {
    private String prefix = "system/info";

    @Autowired
    private ITCustomerInfoService tCustomerInfoService;

    @Autowired
    private CodeGenerateService codeGenerateService;

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询客户信息列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TCustomerInfo tCustomerInfo) {
        startPage();
        List<TCustomerInfo> list = tCustomerInfoService.selectTCustomerInfoList(tCustomerInfo);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TCustomerInfo tCustomerInfo) {
        List<TCustomerInfo> list = tCustomerInfoService.selectTCustomerInfoList(tCustomerInfo);
        ExcelUtil<TCustomerInfo> util = new ExcelUtil<TCustomerInfo>(TCustomerInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 新增客户信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存客户信息
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TCustomerInfo tCustomerInfo) {
        TCustomerInfo lastData=tCustomerInfoService.getLastData();
        String cstno=codeGenerateService.generateCode("CUSTOMER_CODE_RULE",lastData.getCstno(),lastData);
        if(StringUtils.isEmpty(cstno)){
            throw new BusinessException("生成的客户编码不能为空");
        }
        TCustomerInfo queryCus=new TCustomerInfo();
        queryCus.setCstno(cstno);
        List<TCustomerInfo> list=tCustomerInfoService.selectTCustomerInfoList(queryCus);
        if(CollectionUtils.isNotEmpty(list)){
            throw new BusinessException("生成的客户编码重复");
        }
        tCustomerInfo.setCstno(cstno);
        tCustomerInfo.setCreateBy(ShiroUtils.getLoginName());
        tCustomerInfo.setUpdateBy(ShiroUtils.getLoginName());
        tCustomerInfo.setCreateTime(new Date());
        tCustomerInfo.setUpdateTime(new Date());
        tCustomerInfo.setStatus(0);
       return toAjax(tCustomerInfoService.insertTCustomerInfo(tCustomerInfo));
    }

    /**
     * 修改客户信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TCustomerInfo tCustomerInfo = tCustomerInfoService.selectTCustomerInfoById(id);
        mmap.put("tCustomerInfo", tCustomerInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户信息
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TCustomerInfo tCustomerInfo) {
        return toAjax(tCustomerInfoService.updateTCustomerInfo(tCustomerInfo));
    }

    /**
     * 删除客户信息
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tCustomerInfoService.deleteTCustomerInfoByIds(ids));
    }

    @RequiresPermissions("system:info:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<TCustomerInfo> util = new ExcelUtil<TCustomerInfo>(TCustomerInfo.class);
        return util.importTemplateExcel("客户数据");
    }

    /**
     * 导入客户数据
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:user:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TCustomerInfo> util = new ExcelUtil<TCustomerInfo>(TCustomerInfo.class);
        List<TCustomerInfo> cusList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = tCustomerInfoService.importUser(cusList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(TCustomerInfo tCustomerInfo) {
        return tCustomerInfoService.checkPhoneUnique(tCustomerInfo);
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(TCustomerInfo tCustomerInfo) {
        return tCustomerInfoService.checkEmailUnique(tCustomerInfo);
    }

    /**
     * 校验公司名称不能重复
     */
    @PostMapping("/checkUserName")
    @ResponseBody
    public String checkUserName(TCustomerInfo tCustomerInfo) {
        return tCustomerInfoService.checkUserName(tCustomerInfo);
    };

    public  static void main(String[] args){
        String cstno="001";
        Integer b=Integer.valueOf(cstno);
        System.out.println(b+1);
    }

 
}
